package com.badibadi.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.badibadi.adapter.MyPolyAdapter.ViewHolder;
import com.badibadi.infos.Club_Photo_Model;
import com.badibadi.infos.Photo;
import com.badibadi.infos.PhotoModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class Club_Poly_Adapter extends BaseAdapter {

	private static final String TAG = "Club_Poly_Adapter";
	// private int ModelFlags = 0;// 版型标记，默认为0
	Context context;
	private DisplayImageOptions options;

	private List<Photo> mList;
	// 版型 有三种 默认0 1参考 ， 2四个格子
	private String Version = "0";

	private FinalBitmap finalBitmap;

	// 是否加入了该俱乐部（是否是会员）,是否是管理员
	private String isJoin, isAdmin;// 两个一样 1：是,0：否

	private String is_apply;// 是否需要申请表

	public Club_Poly_Adapter(Context ct, List<Photo> list, String isJoin,
			String isAdmin, String is_apply) {
		context = ct;
		mList = list;
		// this.Version = Version;
		this.is_apply = is_apply;
		this.isJoin = isJoin;
		this.isAdmin = isAdmin;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
		finalBitmap = FinalBitmap.create(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		// return mList.get(position).getShow_img();
		return "";
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub

		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (Version.equals("0")) {
			return 0;
		} else if (Version.equals("1"))
			return 1;
		else if (Version.equals("2"))
			return 2;
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
//		   
		ViewHolder holder = new ViewHolder();

		if (convertView == null) {

			// 版型1
			if (getItemViewType(position) == 0) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
					holder.iv_4 = (ImageView) convertView
							.findViewById(R.id.iv_4);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} // 版型2
			else if (getItemViewType(position) == 1) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout1, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
					holder.iv_4 = (ImageView) convertView
							.findViewById(R.id.iv_4);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}// 版型3
			else if (getItemViewType(position) == 2) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout2, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 版型1
		if (getItemViewType(position) == 0) {
			initBanxing1(position, holder);
		} else if (getItemViewType(position) == 1) {
			initBanxing1(position, holder);
		}
		// else if (getItemViewType(position) == 2) {
		// initBanxing2(position, holder);
		// }
		return convertView;
	}

	/**
	 * 1号版型 2号版型都适用
	 * 
	 * @param position
	 * @param holder
	 */
	private void initBanxing1(int position, ViewHolder holder) {
		// TODO Auto-generated method stub
//		   
		if (isJoin.equals("1") || isJoin.equals("0")
				&& mList.get(position).getRoom_status().equals("1")) {

			// 相册名
			holder.photo_name.setText("" + mList.get(position).getName());
			// img_count数量
			holder.img_count.setText("("
					+ mList.get(position).getPhotoImgCount() + ")");
			holder.lock.setVisibility(View.GONE);
			if (mList.get(position).getPhotoImg() != null) {

				// 大图
				if (mList.get(position).getPhotoImg().size() < 1) {
					holder.iv_0.setImageResource(R.drawable.noimage);
					// holder.iv_1.setImageResource(R.drawable.noimage);
					// holder.iv_2.setImageResource(R.drawable.noimage);
					// holder.iv_3.setImageResource(R.drawable.noimage);
					// holder.iv_4.setImageResource(R.drawable.noimage);
					// return ;
				} else {
					try {
						// 大图
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getPhotoImg()
												.get(0).getPath()+Constants.AppmomentImg, holder.iv_0,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				// 第一张小图
				if (mList.get(position).getPhotoImg().size() < 2)

				{
					holder.iv_1.setImageResource(R.drawable.noimage);
				}

				else {

					try {
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getPhotoImg()
												.get(1).getPath()+Constants.Appalbum1, holder.iv_1,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				// 第二张小图
				if (mList.get(position).getPhotoImg().size() < 3) {
					holder.iv_2.setImageResource(R.drawable.noimage);
				} else {

					try {
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getPhotoImg()
												.get(2).getPath()+Constants.Appalbum1, holder.iv_2,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				// 第三张小图
				if (mList.get(position).getPhotoImg().size() < 4) {
					holder.iv_3.setImageResource(R.drawable.noimage);
				} else {

					try {
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getPhotoImg()
												.get(3).getPath()+Constants.Appalbum1, holder.iv_3,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				// 第四章小图
				if (mList.get(position).getPhotoImg().size() < 5) {
					holder.iv_4.setImageResource(R.drawable.noimage);
				} else {

					try {
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getPhotoImg()
												.get(4).getPath()+Constants.Appalbum1, holder.iv_4,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			} else {

				holder.iv_0.setImageResource(R.drawable.noimage);
				holder.iv_1.setImageResource(R.drawable.noimage);
				holder.iv_2.setImageResource(R.drawable.noimage);
				holder.iv_3.setImageResource(R.drawable.noimage);
				holder.iv_4.setImageResource(R.drawable.noimage);

			}

		}
	}

	// private void initBanxing2(int position, ViewHolder holder) {
	// if (mList.get(position).getStatus().equals("1")) {
	// // 相册名
	// holder.photo_name.setText("" + mList.get(position).getPhoto_name());
	// // img_count数量
	// holder.img_count.setText("(" + mList.get(position).getImg_count()
	// + ")");
	// holder.lock.setVisibility(View.GONE);
	// if (mList.get(position).getShow_img() != null) {
	//
	// // 大图
	// if (mList.get(position).getShow_img().size() < 1) {
	// holder.iv_0.setImageResource(R.drawable.noimage);
	// } else {
	// try {
	// // 大图
	// ImageLoader.getInstance().displayImage(
	// Constants.BadiDownImgUrl
	// + "/"
	// + mList.get(position).getShow_img()
	// .get(0).getPath(), holder.iv_0,
	// options);
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// }
	// // 第一张小图
	// if (mList.get(position).getShow_img().size() < 2)
	//
	// {
	// holder.iv_1.setImageResource(R.drawable.noimage);
	// }
	//
	// else {
	//
	// try {
	// ImageLoader.getInstance().displayImage(
	// Constants.BadiDownImgUrl
	// + "/"
	// + mList.get(position).getShow_img()
	// .get(1).getPath(), holder.iv_1,
	// options);
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// }
	// // 第二张小图
	// if (mList.get(position).getShow_img().size() < 3) {
	// holder.iv_2.setImageResource(R.drawable.noimage);
	// } else {
	//
	// try {
	// ImageLoader.getInstance().displayImage(
	// Constants.BadiDownImgUrl
	// + "/"
	// + mList.get(position).getShow_img()
	// .get(2).getPath(), holder.iv_2,
	// options);
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// }
	// // 第三张小图
	// if (mList.get(position).getShow_img().size() < 4) {
	// holder.iv_3.setImageResource(R.drawable.noimage);
	// } else {
	//
	// try {
	// ImageLoader.getInstance().displayImage(
	// Constants.BadiDownImgUrl
	// + "/"
	// + mList.get(position).getShow_img()
	// .get(3).getPath(), holder.iv_3,
	// options);
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// }
	//
	// }
	// else
	// {
	// holder.iv_0.setImageResource(R.drawable.noimage);
	// holder.iv_1.setImageResource(R.drawable.noimage);
	// holder.iv_2.setImageResource(R.drawable.noimage);
	// holder.iv_3.setImageResource(R.drawable.noimage);
	//
	// }
	//
	// }
	//
	// }

	static class ViewHolder {
		// 相册名
		public TextView photo_name;

		// img_count数量
		public TextView img_count;

		ImageView iv_0;

		ImageView iv_1;

		ImageView iv_2;

		ImageView iv_3;

		ImageView iv_4;
		public ImageView lock;

	}

	public String getVersion() {
		return Version;
	}

//	public void setDownPicWH(final ImageView iv, final int newWidth,
//			final int newHeight) {
//		ImageLoader.getInstance().loadImage("", options,
//				new ImageLoadingListener() {
//
//					@Override
//					public void onLoadingStarted(String arg0, View arg1) {
//						// TODO Auto-generated method stub
//
//					}
//
//					@Override
//					public void onLoadingFailed(String arg0, View arg1,
//							FailReason arg2) {
//						// TODO Auto-generated method stub
//
//					}
//
//					@Override
//					public void onLoadingComplete(String arg0, View arg1,
//							Bitmap arg2) {
//						// TODO Auto-generated method stub
//						iv.setImageBitmap(Utils.scaleImg(arg2, newWidth,
//								newHeight));
//					}
//
//					@Override
//					public void onLoadingCancelled(String arg0, View arg1) {
//						// TODO Auto-generated method stub
//
//					}
//				});
//	};

	public void setVersion(String version) {
		Version = version;
	}
}
